package com.fanbai.score.user.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;

import com.fanbai.score.R;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.CommonUtil;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.Utils.PermissionUtils;
import com.fanbai.score.common.Utils.SPUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.Utils.pop.PopCallBack;
import com.fanbai.score.common.Utils.pop.PopPhotoWin;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.http.HttpUtils;
import com.fanbai.score.common.view.CircleImageView;
import com.fanbai.score.user.bean.User_Bean;
import com.fanbai.score.user.http.UserRequestNetwork_Utils;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by acer on 2017/5/13.
 */

public class UserInfo_Activity extends BaseActivity{

    private PermissionUtils perm;
    @BindView(R.id.userActivityUserInfo_iconView)
    CircleImageView iconImg;

    @OnClick({R.id.userActivityUserInfo_iconLayout, R.id.userActivityUserInfo_nameLayout, R.id.userActivityUserInfo_EWMLayout})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.userActivityUserInfo_iconLayout:
                perm = PermissionUtils.getInstance();
                String[] perms = perm.filtRequestPerm(ctx, perm.GROUP_STORAGE, perm.GROUP_CAMERA);//perm.GROUP_CAMERA 可选
                if (perms == null || perms.length == 0) {
                    if (popPhoto == null)
                        popPhoto = new PopPhotoWin(ctx);
                    popPhoto.show(iconImg);
                } else {
                    perm.request(ctx, perms, 1);
                }
                break;
            case R.id.userActivityUserInfo_nameLayout:
                jumpToActivity(EditNickName_Activity.class);
                break;
            case R.id.userActivityUserInfo_EWMLayout:

                break;
        }
    }

    /**
     * 选择的图片的地址
     */
    private String headIconPath;
    private PopPhotoWin popPhoto;

    @Override
    protected int getLayoutID() {
        return R.layout.user_activity_userinfo;
    }

    @Override
    public String getTitleName() {
        return "个人信息";
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
    User_Bean bean;
    @Override
    protected void onResume() {
        super.onResume();
        bean = JsonUtils.toBean(SPUtils.with(ctx).get(SPUtils.KEY_USER_USERINFO, ""), User_Bean.class);
        ImageUtils.show(ctx, bean.getClogoimage(), iconImg, R.mipmap.ic_launcher);
    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getTitleLeftName() {
        return R.mipmap.back_click;
    }

    @Override
    public void onClickTitleLeft() {
        ActivityManager.getInstance().finishActivity();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 获取系统相机或图库的照片(头像)
        if (popPhoto != null)
            popPhoto.getResult(requestCode, data, new PopCallBack<String>() {
                @Override
                public void onResult(String result) {
                    if (!new File(result).exists()) {
                        ToastUtils.ErrorImageToast(ctx, "照片拍摄或获取失败");
                        return;
                    }
                    headIconPath = result;
                    ImageUtils.showInCrop(ctx, headIconPath, iconImg, R.mipmap.ic_launcher);
                    upLoadIcon(headIconPath);
                }
            });
    }

    private void upLoadIcon(String imgPath){
        showDialog();
        UserRequestNetwork_Utils.editUserIcon(ctx, imgPath, new HttpUtils.UploadCallBack() {
            @Override
            public void inProgress(float progress, long total) {

            }

            @Override
            public void onSuccess(String msg, String body) {
                hideDialog();
                User_Bean bean = JsonUtils.toBean(SPUtils.with(ctx).get(SPUtils.KEY_USER_USERINFO, ""), User_Bean.class);
                if(bean != null){
                    bean.setClogoimage(""+body);
                }
                ToastUtils.RightImageToast(ctx, "修改成功");
            }

            @Override
            public void onFailure(String msg, String body) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, ""+msg);
            }

            @Override
            public void onError(String code, Exception e) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, ""+e.getMessage());
            }
        });
    }

    @SuppressLint("Override")
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        perm.onResult(requestCode, grantResults, 1, new PermissionUtils.permissionCallBack() {
            @Override
            public void onResult(boolean isGranted) {
                if (isGranted) {
                    if (popPhoto == null)
                        popPhoto = new PopPhotoWin(ctx);
                    popPhoto.show(iconImg);
                }else{
                    CommonUtil.checkCameraPermission(ctx);
                }
            }
        });
    }
}
